package com.ruoyi.web.controller.eat;

import com.ruoyi.eat.domain.GoodsSpu;
import com.ruoyi.eat.service.IGoodsSpuService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 */
@Controller
@RequestMapping("/food/goodsSpu")
public class GoodsSpuController extends BaseController {
    private String prefix = "eat/goodsSpu";

    @Autowired
    private IGoodsSpuService goodsSpuService;
    @Autowired
    private ISysUserService userService;


    @GetMapping()
    public String goodsSpu() {
        return prefix + "/goodsSpu";
    }


    @GetMapping("view")
    public String view(ModelMap mmap) {
        mmap.put("userList", userService.selectUserListRange(getSysUser(), "01"));
        return prefix + "/goodsSpuView";
    }

    /**
     * 查询商品列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsSpu goodsSpu) {
        if(StringUtils.isEmpty(goodsSpu.getCreateBy())){
            goodsSpu.setCreateBy(getUserId() + "");
        }

        startPage();
        List<GoodsSpu> list = goodsSpuService.selectGoodsSpuList(goodsSpu);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsSpu goodsSpu) {
        List<GoodsSpu> list = goodsSpuService.selectGoodsSpuList(goodsSpu);
        ExcelUtil<GoodsSpu> util = new ExcelUtil<GoodsSpu>(GoodsSpu.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsSpu goodsSpu) {
        goodsSpu.setCreateBy(getUserId() + "");
        return toAjax(goodsSpuService.insertGoodsSpu(goodsSpu));
    }


    /**
     * 商品详情
     */

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsSpu goodsSpu = goodsSpuService.selectGoodsSpuById(id);
        mmap.put("goodsSpu", goodsSpu);
        return prefix + "/detail";
    }

    /**
     * 修改商品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsSpu goodsSpu = goodsSpuService.selectGoodsSpuById(id);
        mmap.put("goodsSpu", goodsSpu);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsSpu goodsSpu) {
        return toAjax(goodsSpuService.updateGoodsSpu(goodsSpu));
    }

    /**
     * 删除商品
     */
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsSpuService.deleteGoodsSpuByIds(ids));
    }
}
